<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="线性表,数据结构,循环链表,单向链表，双向链表">
    <meta name="description" content="C# | Web | html | python |">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
	<meta name="baidu-site-verification" content="code-c9IEbSDR1U" />

	
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>线性表 | X-Y-Z`s Blog</title>
	
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>
    <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>
<meta name="generator" content="Hexo 5.4.0">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>



   <style>
    body{
       background-image: url(/medias/background.JPG);
       background-repeat:no-repeat;
       background-size:cover;
       background-attachment:fixed;
    }
</style>



<body  onload="opener.location.reload()">

	
    <header class="navbar-fixed">

<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?b3593488200a34a873defcaa11fc888a";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>


    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">X-Y-Z`s Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-sitemap" style="zoom: 0.6;"></i>
      
      <span>归类</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>分类</span>
        </a>
      </li>
      
      <li>
        <a href="/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>归档</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/navigate" class="waves-effect waves-light">
      
      <i class="fas fa-location-arrow" style="zoom: 0.6;"></i>
      
      <span>导航</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友链</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">X-Y-Z`s Blog</div>
        <div class="logo-desc">
            
            C# | Web | html | python |
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-sitemap"></i>
			
			归类
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/categories " style="margin-left:50px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:28px" ></i>
			      
		          <span>分类</span>
                  </a>
                </li>
              
                <li>

                  <a href="/archives " style="margin-left:50px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:28px" ></i>
			      
		          <span>归档</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/navigate" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-location-arrow"></i>
			
			Navigate
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/blinkfox/hexo-theme-matery" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork He
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #4e9ef5;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/blinkfox/hexo-theme-matery" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork He" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/0.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">线性表</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

	.toc-widget.card.toc-fixed {
    opacity: 0.85;
	}
    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
		height: 500px;
		overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }
    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">
                                <span class="chip bg-color">数据结构</span>
                            </a>
                        
                            <a href="/tags/%E7%BA%BF%E6%80%A7%E8%A1%A8/">
                                <span class="chip bg-color">线性表</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" class="post-category">
                                数据结构
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2021-10-29
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2022-02-23
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    4.4k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    22 分
                </div>
                

                
            </div>
        </div>
        <hr class="clearfix">

        

        
        <!-- 代码块折行 -->
        <style type="text/css">
            code[class*="language-"], pre[class*="language-"] { white-space: pre-wrap !important; }
        </style>
        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h3 id="0-0-初识"><a href="#0-0-初识" class="headerlink" title="0.0 初识"></a>0.0 初识</h3><p>为了表示每个数据元素与其直接后继数据元素之间的逻辑关系，每个数据元素除了自身的信息以外，还需要存储一个指示其直接后继的信息（后继节点的存储位置）。这两部分信息组成数据元素的存储映像——<mark>节点</mark>。</p>
<p>其中存储元素<code>数据信息</code>的域: <mark>数据域</mark>；存储<code>直接后继</code> 存储位置 的域: <mark>指针域</mark>。<code>n</code>个节点<code>链结</code>成一个链表，为线性表的链式存储结构。</p>
<h3 id="0-1操作习惯"><a href="#0-1操作习惯" class="headerlink" title="0.1操作习惯"></a>0.1操作习惯</h3><ul>
<li><p>整个链表的存取必须从<mark>头指针</mark>开始进行。<code>头指针</code>: 链表中第一个节点的存储位置。</p>
</li>
<li><p>单链表的头元素为单独的一个元素。其数据信息可以什么都不存储，或 保存整个链表的部分信息，</p>
<blockquote>
<p>​        如链表长度；或节点个数。</p>
</blockquote>
</li>
<li><p>要取得第<code>i</code> 个数据元素，必须从头指针出发顺链进行寻找。</p>
<blockquote>
<p>​        因单链表中，任何两个元素的存储位置之间没有固定的联系。每个元素的存储位置都保存在其直接前驱节点的信息里。</p>
</blockquote>
</li>
</ul>
<h3 id="1-顺序表-C"><a href="#1-顺序表-C" class="headerlink" title="1. 顺序表 (C)"></a>1. 顺序表 (C)</h3><p>声明一个结构体。结构体内包含一个数组与一个<code>int</code> 型记录数组长度。然后自动初始化，再根据提示进行查找，删除，插入操作。</p>
<h4 id="1-1-声明区"><a href="#1-1-声明区" class="headerlink" title="1.1 声明区"></a>1.1 声明区</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;malloc.h&gt;</span></span><br><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> maxsize 20<span class="hljs-comment">//字符串的最大长度</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> OK 1	<span class="hljs-comment">//操作OK时返回OK</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> <span class="hljs-meta-keyword">error</span> -1<span class="hljs-comment">//操作不OK时返回error</span></span><br><br><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Lnode</span> {</span><br>	<span class="hljs-keyword">int</span> *elem;<br>	<span class="hljs-keyword">int</span> length;<br>}Lnode;<span class="hljs-comment">//定义结构体 Lnode</span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">list_init</span><span class="hljs-params">(Lnode* l)</span></span>;<span class="hljs-comment">//数组初始化,给数组的每一项赋值。</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">list_print</span><span class="hljs-params">(Lnode*l)</span></span>;<span class="hljs-comment">//把整个数组打印出来,打印格式为: elem [index]: value。</span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">list_find</span><span class="hljs-params">(Lnode* l, <span class="hljs-keyword">int</span> value)</span></span>;<span class="hljs-comment">//根据 value ，返回这个值的序列或 -1 。</span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">list_insert</span><span class="hljs-params">(Lnode* l,<span class="hljs-keyword">int</span> value,<span class="hljs-keyword">int</span> index )</span></span>;<span class="hljs-comment">//在数组末尾插入 value，超出最大范围返回-1。</span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">list_delete</span><span class="hljs-params">(Lnode *l, <span class="hljs-keyword">int</span> index)</span></span>; <span class="hljs-comment"><code class="language-hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;malloc.h&gt;</span></span><br><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> maxsize 20<span class="hljs-comment">//字符串的最大长度</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> OK 1	<span class="hljs-comment">//操作OK时返回OK</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> <span class="hljs-meta-keyword">error</span> -1<span class="hljs-comment">//操作不OK时返回error</span></span><br><br><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Lnode</span> {</span><br>	<span class="hljs-keyword">int</span> *elem;<br>	<span class="hljs-keyword">int</span> length;<br>}Lnode;<span class="hljs-comment">//定义结构体 Lnode</span><br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">list_init</span><span class="hljs-params">(Lnode* l)</span></span>;<span class="hljs-comment">//数组初始化,给数组的每一项赋值。</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">list_print</span><span class="hljs-params">(Lnode*l)</span></span>;<span class="hljs-comment">//把整个数组打印出来,打印格式为: elem [index]: value。</span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">list_find</span><span class="hljs-params">(Lnode* l, <span class="hljs-keyword">int</span> value)</span></span>;<span class="hljs-comment">//根据 value ，返回这个值的序列或 -1 。</span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">list_insert</span><span class="hljs-params">(Lnode* l,<span class="hljs-keyword">int</span> value,<span class="hljs-keyword">int</span> index )</span></span>;<span class="hljs-comment">//在数组末尾插入 value，超出最大范围返回-1。</span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">list_delete</span><span class="hljs-params">(Lnode *l, <span class="hljs-keyword">int</span> index)</span></span>; <span class="hljs-comment">//删除传入序列对应的值。//函数预声明。避免顺序问题</span><br></code></pre></td></tr></tbody></table></figure>

<h4 id="1-2-主函数"><a href="#1-2-主函数" class="headerlink" title="1.2 主函数"></a>1.2 主函数</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br>	Lnode *ln = (Lnode*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(Lnode));<br>	list_init(ln);<br>	<span class="hljs-comment">//初始化</span><br>	list_print(ln);<br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n请输入要查找的值,将会输出他的序列:\n"</span>);<br>	<span class="hljs-keyword">int</span> value;<br>	<span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>, &amp;value);<br>	<span class="hljs-comment">//查找</span><br>	<span class="hljs-keyword">int</span> term=list_find(ln, value) ;<br>	<span class="hljs-keyword">if</span> (term&lt; <span class="hljs-number">0</span>)<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"查找的值不存在"</span>);<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"所查找的值的序列为%d\n"</span>, term);<br>	}<br>	<span class="hljs-comment">//删除</span><br>	list_print(ln);<br>	<span class="hljs-keyword">int</span> index;<br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n请输入删除的序列:\n"</span>);<br>	<span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>, &amp;index);<br>	<span class="hljs-built_in">printf</span>(((list_delete(ln, index)==OK)?<span class="hljs-string">"删除成功"</span>:<span class="hljs-string">"删除失败"</span>));<br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n删除后数组:"</span>);<br>	list_print(ln);<br>	<span class="hljs-comment">//插入</span><br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n输入将要插入的值与序列，之间以空格隔开:\n"</span>);<br>	value = <span class="hljs-literal">NULL</span>;<br>	index = <span class="hljs-literal">NULL</span>;<br>	<span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d %d"</span>, &amp;value,&amp;index);<br>	<span class="hljs-built_in">printf</span>(((list_insert(ln,value,index<span class="hljs-number">-1</span>)==OK)?<span class="hljs-string">"插入成功"</span>:<span class="hljs-string">"插入失败"</span>));<br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br><br>	Lnode *ln = (Lnode*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(Lnode));<br>	list_init(ln);<br>	<span class="hljs-comment">//初始化</span><br>	list_print(ln);<br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n请输入要查找的值,将会输出他的序列:\n"</span>);<br>	<span class="hljs-keyword">int</span> value;<br>	<span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>, &amp;value);<br>	<span class="hljs-comment">//查找</span><br>	<span class="hljs-keyword">int</span> term=list_find(ln, value) ;<br>	<span class="hljs-keyword">if</span> (term&lt; <span class="hljs-number">0</span>)<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"查找的值不存在"</span>);<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"所查找的值的序列为%d\n"</span>, term);<br>	}<br>	<span class="hljs-comment">//删除</span><br>	list_print(ln);<br>	<span class="hljs-keyword">int</span> index;<br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n请输入删除的序列:\n"</span>);<br>	<span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d"</span>, &amp;index);<br>	<span class="hljs-built_in">printf</span>(((list_delete(ln, index)==OK)?<span class="hljs-string">"删除成功"</span>:<span class="hljs-string">"删除失败"</span>));<br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n删除后数组:"</span>);<br>	list_print(ln);<br>	<span class="hljs-comment">//插入</span><br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n输入将要插入的值与序列，之间以空格隔开:\n"</span>);<br>	value = <span class="hljs-literal">NULL</span>;<br>	index = <span class="hljs-literal">NULL</span>;<br>	<span class="hljs-built_in">scanf</span>(<span class="hljs-string">"%d %d"</span>, &amp;value,&amp;index);<br>	<span class="hljs-built_in">printf</span>(((list_insert(ln,value,index<span class="hljs-number">-1</span>)==OK)?<span class="hljs-string">"插入成功"</span>:<span class="hljs-string">"插入失败"</span>));<br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n插入后数组为:\n"</span>);<br>	list_print(ln);<br>}<br></code></pre></td></tr></tbody></table></figure>

<h4 id="1-3-初始化函数"><a href="#1-3-初始化函数" class="headerlink" title="1.3 初始化函数"></a>1.3 初始化函数</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">list_init</span><span class="hljs-params">(Lnode* l)</span> </span>{<br>	<span class="hljs-comment">//根据传入的指针，初始化此结构体内数组；</span><br>	l-&gt;elem = (<span class="hljs-keyword">int</span>*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">int</span>[maxsize]));<br><br>	<span class="hljs-keyword">if</span> (!l-&gt;elem) <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);<br>	l-&gt;length = <span class="hljs-number">0</span>;<br><br>	<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; maxsize; i++)<br>	{<br>		(l-&gt;elem)[i] = i * <span class="hljs-number"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">list_init</span><span class="hljs-params">(Lnode* l)</span> </span>{<br>	<span class="hljs-comment">//根据传入的指针，初始化此结构体内数组；</span><br>	l-&gt;elem = (<span class="hljs-keyword">int</span>*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">int</span>[maxsize]));<br><br>	<span class="hljs-keyword">if</span> (!l-&gt;elem) <span class="hljs-built_in">exit</span>(<span class="hljs-number">0</span>);<br>	l-&gt;length = <span class="hljs-number">0</span>;<br><br>	<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; maxsize; i++)<br>	{<br>		(l-&gt;elem)[i] = i * <span class="hljs-number">2</span>;<br>		l->length++;<br>	}<br>}<br></code></pre></td></tr></tbody></table></figure>

<h4 id="1-4输出函数"><a href="#1-4输出函数" class="headerlink" title="1.4输出函数"></a>1.4输出函数</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">list_print</span><span class="hljs-params">(Lnode*l)</span> </span>{<br>    <span class="hljs-comment">//格式:每五个换行</span><br>    <span class="hljs-comment">//	   elem[0  ]:0</span><br>	<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; l-&gt;length; i++)<br>	{<br>		<span class="hljs-keyword">if</span> (i % <span class="hljs-number">5</span> == <span class="hljs-number">0</span>)<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n"</span>);<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">list_print</span><span class="hljs-params">(Lnode*l)</span> </span>{<br>    <span class="hljs-comment">//格式:每五个换行</span><br>    <span class="hljs-comment">//	   elem[0  ]:0</span><br>	<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; l-&gt;length; i++)<br>	{<br>		<span class="hljs-keyword">if</span> (i % <span class="hljs-number">5</span> == <span class="hljs-number">0</span>)<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n"</span>);<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"elem[%-3d]:%-3d  "</span>,i, l->elem[i]);<br>	}<br>}<br></code></pre></td></tr></tbody></table></figure>

<h4 id="1-5-查找"><a href="#1-5-查找" class="headerlink" title="1.5 查找"></a>1.5 查找</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">list_find</span><span class="hljs-params">(Lnode* l,<span class="hljs-keyword">int</span> value)</span> </span>{<br>	<span class="hljs-comment">//传入值value，输出序列至x；</span><br>	<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; l-&gt;length; i++)<br>	{<br>		<span class="hljs-keyword">if</span> (l-&gt;elem[i] == value) {<br>			<span class="hljs-keyword">return</span> i;<br>		}<br>	}<br>	<span class="hljs-keyword"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">list_find</span><span class="hljs-params">(Lnode* l,<span class="hljs-keyword">int</span> value)</span> </span>{<br>	<span class="hljs-comment">//传入值value，输出序列至x；</span><br>	<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; l-&gt;length; i++)<br>	{<br>		<span class="hljs-keyword">if</span> (l-&gt;elem[i] == value) {<br>			<span class="hljs-keyword">return</span> i;<br>		}<br>	}<br>	<span class="hljs-keyword">return</span> error;<br>}<br></code></pre></td></tr></tbody></table></figure>

<h4 id="1-6-插入"><a href="#1-6-插入" class="headerlink" title="1.6 插入"></a>1.6 插入</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">list_insert</span><span class="hljs-params">(Lnode* l,<span class="hljs-keyword">int</span> value,<span class="hljs-keyword">int</span> index )</span> </span>{<br>    <span class="hljs-comment">//刚满,或不足,或序列不合规返回error。</span><br>	<span class="hljs-keyword">if</span> (l-&gt;length == maxsize||index&gt;maxsize||index&lt;<span class="hljs-number">0</span>) {<br>		<span class="hljs-keyword">return</span> error;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		l-&gt;length++;<br>		<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = l-&gt;length;  i &gt; index;  i--)<br>		{<br>			l-&gt;elem[i] = l-&gt;elem[i - <span class="hljs-number">1</span>];<br>			l-&gt;elem[index] = value;<br>		}<br>		l-&gt;elem[l-&gt;length<span class="hljs-number">-1</span>] = value;<br>		<span class="hljs-keyword"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">list_insert</span><span class="hljs-params">(Lnode* l,<span class="hljs-keyword">int</span> value,<span class="hljs-keyword">int</span> index )</span> </span>{<br>    <span class="hljs-comment">//刚满,或不足,或序列不合规返回error。</span><br>	<span class="hljs-keyword">if</span> (l-&gt;length == maxsize||index&gt;maxsize||index&lt;<span class="hljs-number">0</span>) {<br>		<span class="hljs-keyword">return</span> error;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		l-&gt;length++;<br>		<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = l-&gt;length;  i &gt; index;  i--)<br>		{<br>			l-&gt;elem[i] = l-&gt;elem[i - <span class="hljs-number">1</span>];<br>			l-&gt;elem[index] = value;<br>		}<br>		l-&gt;elem[l-&gt;length<span class="hljs-number">-1</span>] = value;<br>		<span class="hljs-keyword">return</span> OK;<br>	}<br>}<br></code></pre></td></tr></tbody></table></figure>
<h4 id="1-7-删除"><a href="#1-7-删除" class="headerlink" title="1.7 删除"></a>1.7 删除</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">list_delete</span><span class="hljs-params">(Lnode *l, <span class="hljs-keyword">int</span> index)</span> </span>{<br>	<span class="hljs-keyword">if</span> (index&lt;<span class="hljs-number">0</span> || index&gt;maxsize - <span class="hljs-number">2</span>) {<br>		<span class="hljs-keyword">return</span> error;<br>	}<br>	<span class="hljs-keyword">else</span><br>	{<br>		<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = index; j &lt; l-&gt;length - <span class="hljs-number">1</span>; j++)<br>		{<br>			l-&gt;elem[j] = l-&gt;elem[j + <span class="hljs-number">1</span>];<br>		}<br>		l-&gt;length--;<br>		<span class="hljs-keyword"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">list_delete</span><span class="hljs-params">(Lnode *l, <span class="hljs-keyword">int</span> index)</span> </span>{<br>	<span class="hljs-keyword">if</span> (index&lt;<span class="hljs-number">0</span> || index&gt;maxsize - <span class="hljs-number">2</span>) {<br>		<span class="hljs-keyword">return</span> error;<br>	}<br>	<span class="hljs-keyword">else</span><br>	{<br>		<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = index; j &lt; l-&gt;length - <span class="hljs-number">1</span>; j++)<br>		{<br>			l-&gt;elem[j] = l-&gt;elem[j + <span class="hljs-number">1</span>];<br>		}<br>		l-&gt;length--;<br>		<span class="hljs-keyword">return</span> OK;<br>	}<br>}<br></code></pre></td></tr></tbody></table></figure>

<h3 id="2-单链表-C"><a href="#2-单链表-C" class="headerlink" title="2. 单链表 (C)"></a>2. 单链表 (C)</h3><ul>
<li>代码</li>
</ul>
<p>查找不想写了，在顺序表中有。</p>
<h4 id="2-1-声明"><a href="#2-1-声明" class="headerlink" title="2.1 声明"></a>2.1 声明</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;malloc.h&gt;</span> </span><br><br><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">TEST</span> {</span><br>	<span class="hljs-keyword">int</span> num;<br>	<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">TEST</span> *<span class="hljs-title">next</span>;</span><br>} TEST;<br><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">init</span><span class="hljs-params">(TEST * head)</span></span>;<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">create</span><span class="hljs-params">(TEST * head, <span class="hljs-keyword">int</span> n)</span></span>;<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">deleteEle</span><span class="hljs-params">(TEST *head, <span class="hljs-keyword">int</span> pos)</span></span>; <br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">destroy</span><span class="hljs-params">(TEST* head)</span></span>;<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">connect</span><span class="hljs-params">(TEST* a, TEST* b)</span></span>;<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">insertEle</span><span class="hljs-params">(TEST* head, <span class="hljs-keyword">int</span> pos, <span class="hljs-keyword">int</span> value)</span></span>;<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printNode</span><span class="hljs-params"><code class="language-hljs c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;malloc.h&gt;</span> </span><br><br><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">TEST</span> {</span><br>	<span class="hljs-keyword">int</span> num;<br>	<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">TEST</span> *<span class="hljs-title">next</span>;</span><br>} TEST;<br><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">init</span><span class="hljs-params">(TEST * head)</span></span>;<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">create</span><span class="hljs-params">(TEST * head, <span class="hljs-keyword">int</span> n)</span></span>;<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">deleteEle</span><span class="hljs-params">(TEST *head, <span class="hljs-keyword">int</span> pos)</span></span>; <br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">destroy</span><span class="hljs-params">(TEST* head)</span></span>;<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">connect</span><span class="hljs-params">(TEST* a, TEST* b)</span></span>;<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">insertEle</span><span class="hljs-params">(TEST* head, <span class="hljs-keyword">int</span> pos, <span class="hljs-keyword">int</span> value)</span></span>;<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printNode</span><span class="hljs-params">(TEST* head)</span></span>;<br><br></code></pre></td></tr></tbody></table></figure>

<h4 id="2-2-main函数"><a href="#2-2-main函数" class="headerlink" title="2.2 main函数"></a>2.2 main函数</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br>	TEST * head = <span class="hljs-literal">NULL</span>;<br>	<span class="hljs-comment">//链表头</span><br>	<span class="hljs-keyword">int</span> n=<span class="hljs-number">0</span>;<br>	<span class="hljs-keyword">int</span> pos=<span class="hljs-number">1</span>;<br><br>	<span class="hljs-keyword">if</span> (init(&amp;head) == <span class="hljs-number">0</span>) {<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"创建失败"</span>);<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>	}<br>    <span class="hljs-keyword">else</span>{<br>	    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"请输入链表节点数:\n"</span>);<br>		scanf_s(<span class="hljs-string">"%d"</span>, &amp;n);<br>	    <span class="hljs-keyword">if</span> (create(&amp;head, n) == <span class="hljs-number">0</span>) {<br>			<span class="hljs-built_in">printf</span>(<span class="hljs-string">"添加数据失败"</span>);<br>			<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>		}<br>    }<br>	printNode(head);<br>	<span class="hljs-comment">//输出</span><br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n\n再添加一个\n"</span>);<br>    <span class="hljs-comment">//插入</span><br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"请输入插入位置（0~%d之间）:\n"</span>,head-&gt;num+<span class="hljs-number">2</span>);	<br>	scanf_s(<span class="hljs-string">"%d"</span>, &amp;pos);<br>	<span class="hljs-keyword">if</span> (pos&lt;<span class="hljs-number">1</span> || pos&gt;head-&gt;num + <span class="hljs-number">2</span>) {<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"输入有误,将跳过插入操作。"</span>);<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">int</span> value = <span class="hljs-number">0</span>;<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"输入插入的值(别输太大)：\n"</span>);<br>		scanf_s(<span class="hljs-string">"%d"</span>, &amp;value);<br>		insertEle(&amp;head, pos, value);<br>	}<br>	printNode(head);<br>	<span class="hljs-comment">//输出</span><br>	pos = <span class="hljs-number">0</span>;<br>    <span class="hljs-comment">//删除</span><br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"输入要删除的索引号(1~%d):\n"</span>, head-&gt;num);<br>	scanf_s(<span class="hljs-string">"%d"</span>, &amp;pos);<br>	<span class="hljs-keyword">if</span> (pos&lt;<span class="hljs-number">1</span> || pos&gt;head-&gt;num) {<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"输的什么鬼！，直接跳过删除。"</span>);<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		deleteEle(&amp;head, pos);<br>		<span class="hljs-comment">//删除一个元素</span><br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n删除一个数据后:\n"</span>);<br>	}<br>	printNode(head);<br>	<span class="hljs-comment"><code class="language-hljs c"><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{<br>	TEST * head = <span class="hljs-literal">NULL</span>;<br>	<span class="hljs-comment">//链表头</span><br>	<span class="hljs-keyword">int</span> n=<span class="hljs-number">0</span>;<br>	<span class="hljs-keyword">int</span> pos=<span class="hljs-number">1</span>;<br><br>	<span class="hljs-keyword">if</span> (init(&amp;head) == <span class="hljs-number">0</span>) {<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"创建失败"</span>);<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>	}<br>    <span class="hljs-keyword">else</span>{<br>	    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"请输入链表节点数:\n"</span>);<br>		scanf_s(<span class="hljs-string">"%d"</span>, &amp;n);<br>	    <span class="hljs-keyword">if</span> (create(&amp;head, n) == <span class="hljs-number">0</span>) {<br>			<span class="hljs-built_in">printf</span>(<span class="hljs-string">"添加数据失败"</span>);<br>			<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>		}<br>    }<br>	printNode(head);<br>	<span class="hljs-comment">//输出</span><br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n\n再添加一个\n"</span>);<br>    <span class="hljs-comment">//插入</span><br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"请输入插入位置（0~%d之间）:\n"</span>,head-&gt;num+<span class="hljs-number">2</span>);	<br>	scanf_s(<span class="hljs-string">"%d"</span>, &amp;pos);<br>	<span class="hljs-keyword">if</span> (pos&lt;<span class="hljs-number">1</span> || pos&gt;head-&gt;num + <span class="hljs-number">2</span>) {<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"输入有误,将跳过插入操作。"</span>);<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">int</span> value = <span class="hljs-number">0</span>;<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"输入插入的值(别输太大)：\n"</span>);<br>		scanf_s(<span class="hljs-string">"%d"</span>, &amp;value);<br>		insertEle(&amp;head, pos, value);<br>	}<br>	printNode(head);<br>	<span class="hljs-comment">//输出</span><br>	pos = <span class="hljs-number">0</span>;<br>    <span class="hljs-comment">//删除</span><br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">"输入要删除的索引号(1~%d):\n"</span>, head-&gt;num);<br>	scanf_s(<span class="hljs-string">"%d"</span>, &amp;pos);<br>	<span class="hljs-keyword">if</span> (pos&lt;<span class="hljs-number">1</span> || pos&gt;head-&gt;num) {<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"输的什么鬼！，直接跳过删除。"</span>);<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		deleteEle(&amp;head, pos);<br>		<span class="hljs-comment">//删除一个元素</span><br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n删除一个数据后:\n"</span>);<br>	}<br>	printNode(head);<br>	<span class="hljs-comment">//输出</span><br>}<br><br></code></pre></td></tr></tbody></table></figure>

<h4 id="2-3-初始化"><a href="#2-3-初始化" class="headerlink" title="2.3 初始化"></a>2.3 初始化</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">init</span><span class="hljs-params">(TEST * head)</span> </span>{<br>	<span class="hljs-comment">//加个链表头。</span><br>	head-&gt;next = <span class="hljs-literal">NULL</span>;<br>	head-&gt;num = <span class="hljs-number">0</span>;<br>	<span class="hljs-keyword">if</span> (head == <span class="hljs-literal">NULL</span>) {<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">return</span> <span class="hljs-number"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">init</span><span class="hljs-params">(TEST * head)</span> </span>{<br>	<span class="hljs-comment">//加个链表头。</span><br>	head-&gt;next = <span class="hljs-literal">NULL</span>;<br>	head-&gt;num = <span class="hljs-number">0</span>;<br>	<span class="hljs-keyword">if</span> (head == <span class="hljs-literal">NULL</span>) {<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	}<br>}<br></code></pre></td></tr></tbody></table></figure>

<h4 id="2-4-创建"><a href="#2-4-创建" class="headerlink" title="2.4 创建"></a>2.4 创建</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">create</span><span class="hljs-params">(TEST * head, <span class="hljs-keyword">int</span> n)</span> </span>{<br>	<span class="hljs-comment">//链表添加数据，共 n 个</span><br>	TEST *q;<br>	TEST* p;<br>	head-&gt;num = n;<br>	q = head;<br><br>	<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++) {<br>		p = (struct TEST*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(TEST));<br>		p-&gt;num = i;<br>		p-&gt;next = <span class="hljs-literal">NULL</span>;<br>		q-&gt;next = p;<br>		q = p;<br>	}<br>	<span class="hljs-keyword">if</span> ((head)-&gt;num != n) {<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">return</span> <span class="hljs-number"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">create</span><span class="hljs-params">(TEST * head, <span class="hljs-keyword">int</span> n)</span> </span>{<br>	<span class="hljs-comment">//链表添加数据，共 n 个</span><br>	TEST *q;<br>	TEST* p;<br>	head-&gt;num = n;<br>	q = head;<br><br>	<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++) {<br>		p = (struct TEST*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(TEST));<br>		p-&gt;num = i;<br>		p-&gt;next = <span class="hljs-literal">NULL</span>;<br>		q-&gt;next = p;<br>		q = p;<br>	}<br>	<span class="hljs-keyword">if</span> ((head)-&gt;num != n) {<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	}<br>}<br></code></pre></td></tr></tbody></table></figure>

<h4 id="2-5-打印输出"><a href="#2-5-打印输出" class="headerlink" title="2.5 打印输出"></a>2.5 打印输出</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printNode</span><span class="hljs-params">(TEST* head)</span> </span>{<br>	<span class="hljs-comment">//遍历输出打印.</span><br><br>	<span class="hljs-keyword">if</span> (head-&gt;num == <span class="hljs-number">0</span>) {<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n\n一个空链表。没啥输出的。\n"</span>);<br>		<span class="hljs-keyword">return</span>;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>;<br>		<span class="hljs-keyword">for</span> (TEST* i = head-&gt;next; j &lt; head-&gt;num; i = i-&gt;next) {<br>			<span class="hljs-keyword">if</span> (i != <span class="hljs-literal">NULL</span>) {<br>				<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d "</span>, i-&gt;num);<br>				j++;<br>			}<br>			<span class="hljs-keyword">else</span> {<br>				<span class="hljs-keyword">break</span>;<br>			}<br>		}<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printNode</span><span class="hljs-params">(TEST* head)</span> </span>{<br>	<span class="hljs-comment">//遍历输出打印.</span><br><br>	<span class="hljs-keyword">if</span> (head-&gt;num == <span class="hljs-number">0</span>) {<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n\n一个空链表。没啥输出的。\n"</span>);<br>		<span class="hljs-keyword">return</span>;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>;<br>		<span class="hljs-keyword">for</span> (TEST* i = head-&gt;next; j &lt; head-&gt;num; i = i-&gt;next) {<br>			<span class="hljs-keyword">if</span> (i != <span class="hljs-literal">NULL</span>) {<br>				<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d "</span>, i-&gt;num);<br>				j++;<br>			}<br>			<span class="hljs-keyword">else</span> {<br>				<span class="hljs-keyword">break</span>;<br>			}<br>		}<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n"</span>);<br>	}<br>}<br></code></pre></td></tr></tbody></table></figure>

<h4 id="2-6-插入"><a href="#2-6-插入" class="headerlink" title="2.6 插入"></a>2.6 插入</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">insertEle</span><span class="hljs-params">(TEST* head, <span class="hljs-keyword">int</span> pos, <span class="hljs-keyword">int</span> value)</span> </span>{<br>	TEST* q = head;<br>	TEST* p = q-&gt;next;<br><br>	<span class="hljs-keyword">if</span> (pos &lt;= ((head)-&gt;num + <span class="hljs-number">1</span>) &amp;&amp; pos &gt; <span class="hljs-number">0</span>) {<br>		pos--;<br>		<span class="hljs-keyword">while</span> (pos != <span class="hljs-number">0</span> &amp;&amp; p != <span class="hljs-literal">NULL</span>) {<br>			q = p;<br>			p = q-&gt;next;<br>			pos--;<br>		}<br>		<span class="hljs-comment">//找位置			</span><br>		q-&gt;next = (TEST*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(struct TEST));<br>		q-&gt;next-&gt;next = p;<br>		q-&gt;next-&gt;num = value;<br>		(head)-&gt;num++;<br>		<span class="hljs-comment">//插数据。</span><br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">return</span> <span class="hljs-number"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">insertEle</span><span class="hljs-params">(TEST* head, <span class="hljs-keyword">int</span> pos, <span class="hljs-keyword">int</span> value)</span> </span>{<br>	TEST* q = head;<br>	TEST* p = q-&gt;next;<br><br>	<span class="hljs-keyword">if</span> (pos &lt;= ((head)-&gt;num + <span class="hljs-number">1</span>) &amp;&amp; pos &gt; <span class="hljs-number">0</span>) {<br>		pos--;<br>		<span class="hljs-keyword">while</span> (pos != <span class="hljs-number">0</span> &amp;&amp; p != <span class="hljs-literal">NULL</span>) {<br>			q = p;<br>			p = q-&gt;next;<br>			pos--;<br>		}<br>		<span class="hljs-comment">//找位置			</span><br>		q-&gt;next = (TEST*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(struct TEST));<br>		q-&gt;next-&gt;next = p;<br>		q-&gt;next-&gt;num = value;<br>		(head)-&gt;num++;<br>		<span class="hljs-comment">//插数据。</span><br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>	}<br>}<br></code></pre></td></tr></tbody></table></figure>

<h4 id="2-7-删除"><a href="#2-7-删除" class="headerlink" title="2.7 删除"></a>2.7 删除</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">deleteEle</span><span class="hljs-params">(TEST *head, <span class="hljs-keyword">int</span> pos)</span> </span>{<br>	<span class="hljs-comment">//删除指定的项.</span><br>	TEST* p;<br>	TEST* q;<br>	q = head;<br>	p = q-&gt;next;<br><br>	<span class="hljs-keyword">if</span> (pos == <span class="hljs-number">1</span>) {<br>		head-&gt;next = p-&gt;next;<br>		<span class="hljs-built_in">free</span>(p);<br>		head-&gt;num--;<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	}<br>	<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (pos &gt; head-&gt;num) {<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"输入序列超出索引。"</span>);<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">do</span><br>		{<br>			q = p;<br>			p = q-&gt;next;<br>			pos--;<br>		} <span class="hljs-keyword">while</span> (p != <span class="hljs-literal">NULL</span> &amp;&amp; pos != <span class="hljs-number">1</span>);<br>		<span class="hljs-keyword">if</span> (pos == <span class="hljs-number">1</span>)<br>		{<br>			q-&gt;next = p-&gt;next;<br>			head-&gt;num--;<br>			<span class="hljs-built_in">free</span>(p);<br>			<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>		}<br>		<span class="hljs-keyword">else</span> {<br>			<span class="hljs-built_in">printf</span>(<span class="hljs-string">"链表出现问题!"</span>);<br>			<span class="hljs-keyword">return</span> <span class="hljs-number"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">deleteEle</span><span class="hljs-params">(TEST *head, <span class="hljs-keyword">int</span> pos)</span> </span>{<br>	<span class="hljs-comment">//删除指定的项.</span><br>	TEST* p;<br>	TEST* q;<br>	q = head;<br>	p = q-&gt;next;<br><br>	<span class="hljs-keyword">if</span> (pos == <span class="hljs-number">1</span>) {<br>		head-&gt;next = p-&gt;next;<br>		<span class="hljs-built_in">free</span>(p);<br>		head-&gt;num--;<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	}<br>	<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (pos &gt; head-&gt;num) {<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">"输入序列超出索引。"</span>);<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">do</span><br>		{<br>			q = p;<br>			p = q-&gt;next;<br>			pos--;<br>		} <span class="hljs-keyword">while</span> (p != <span class="hljs-literal">NULL</span> &amp;&amp; pos != <span class="hljs-number">1</span>);<br>		<span class="hljs-keyword">if</span> (pos == <span class="hljs-number">1</span>)<br>		{<br>			q-&gt;next = p-&gt;next;<br>			head-&gt;num--;<br>			<span class="hljs-built_in">free</span>(p);<br>			<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>		}<br>		<span class="hljs-keyword">else</span> {<br>			<span class="hljs-built_in">printf</span>(<span class="hljs-string">"链表出现问题!"</span>);<br>			<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>		}<br>	}<br>}<br><br></code></pre></td></tr></tbody></table></figure>

<h4 id="2-8-销毁"><a href="#2-8-销毁" class="headerlink" title="2.8 销毁"></a>2.8 销毁</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">destroy</span><span class="hljs-params">(TEST* head)</span> </span>{<br>	<span class="hljs-comment">//全部销毁</span><br>	TEST* p = (head)-&gt;next;<br>	TEST* q = <span class="hljs-literal">NULL</span>;<br><br>	<span class="hljs-keyword">if</span> (p == <span class="hljs-literal">NULL</span>) {<br>		<span class="hljs-built_in">free</span>(head);<br>		head = <span class="hljs-literal">NULL</span>;<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">do</span> {<br>			q = p;<br>			p = q-&gt;next;<br>			<span class="hljs-built_in">free</span>(q);<br>			(head)-&gt;num--;<br>		} <span class="hljs-keyword">while</span> (p != <span class="hljs-literal">NULL</span>);<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	}<br>	<span class="hljs-keyword">return</span> <span class="hljs-number"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">destroy</span><span class="hljs-params">(TEST* head)</span> </span>{<br>	<span class="hljs-comment">//全部销毁</span><br>	TEST* p = (head)-&gt;next;<br>	TEST* q = <span class="hljs-literal">NULL</span>;<br><br>	<span class="hljs-keyword">if</span> (p == <span class="hljs-literal">NULL</span>) {<br>		<span class="hljs-built_in">free</span>(head);<br>		head = <span class="hljs-literal">NULL</span>;<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	}<br>	<span class="hljs-keyword">else</span> {<br>		<span class="hljs-keyword">do</span> {<br>			q = p;<br>			p = q-&gt;next;<br>			<span class="hljs-built_in">free</span>(q);<br>			(head)-&gt;num--;<br>		} <span class="hljs-keyword">while</span> (p != <span class="hljs-literal">NULL</span>);<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	}<br>	<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>}<br></code></pre></td></tr></tbody></table></figure>

<h4 id="2-9-链表连接"><a href="#2-9-链表连接" class="headerlink" title="2.9 链表连接"></a>2.9 链表连接</h4><figure class="highlight c"><table><tbody><tr><td class="code"><pre class=" language-hljs c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">connect</span><span class="hljs-params">(TEST* a, TEST* b)</span> </span>{<br>	<span class="hljs-comment">// 把b接在a后边。</span><br>	TEST*  q,*p;<br>	p = a;<br>	<span class="hljs-keyword"><code class="language-hljs c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">connect</span><span class="hljs-params">(TEST* a, TEST* b)</span> </span>{<br>	<span class="hljs-comment">// 把b接在a后边。</span><br>	TEST*  q,*p;<br>	p = a;<br>	<span class="hljs-keyword">while</span>(p->next)<br>	{<br>		p = p->next;<br>	}<br>	p->next = b->next;<br>	a->num += b->num;<br>}<br></code></pre></td></tr></tbody></table></figure>

<h3 id="3-双向链表-Java"><a href="#3-双向链表-Java" class="headerlink" title="3. 双向链表 (Java)"></a>3. 双向链表 (Java)</h3><p>任何操作都需要考虑节点的先驱、后驱。</p>
<h4 id="3-1-链表结构"><a href="#3-1-链表结构" class="headerlink" title="3.1 链表结构"></a>3.1 链表结构</h4><figure class="highlight java"><table><tbody><tr><td class="code"><pre class=" language-hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> last 上个节点</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> next 下个链表</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> value 节点值</span><br><span class="hljs-comment">    */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BothDirLinkList</span> </span>{<br>    BothDirLinkList last;<br>    BothDirLinkList next;<br>    String value;<br>	<span class="hljs-comment">//无参构造</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">BothDirLinkList</span><span class="hljs-params">()</span> </span>{<br>    }<br>	<span class="hljs-comment">//全参构造</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">BothDirLinkList</span><span class="hljs-params">(BothDirLinkList last, BothDirLinkList next, String value)</span> </span>{<br>        <span class="hljs-keyword">this</span>.last = last;<br>        <span class="hljs-keyword">this</span>.next = next;<br>        <span class="hljs-keyword"><code class="language-hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> last 上个节点</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> next 下个链表</span><br><span class="hljs-comment">    * <span class="hljs-doctag">@param</span> value 节点值</span><br><span class="hljs-comment">    */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BothDirLinkList</span> </span>{<br>    BothDirLinkList last;<br>    BothDirLinkList next;<br>    String value;<br>	<span class="hljs-comment">//无参构造</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">BothDirLinkList</span><span class="hljs-params">()</span> </span>{<br>    }<br>	<span class="hljs-comment">//全参构造</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">BothDirLinkList</span><span class="hljs-params">(BothDirLinkList last, BothDirLinkList next, String value)</span> </span>{<br>        <span class="hljs-keyword">this</span>.last = last;<br>        <span class="hljs-keyword">this</span>.next = next;<br>        <span class="hljs-keyword">this</span>.value = value;<br>    }<br>}<br></code></pre></td></tr></tbody></table></figure>



<h4 id="3-2-创建"><a href="#3-2-创建" class="headerlink" title="3.2 创建"></a>3.2 创建</h4><figure class="highlight java"><table><tbody><tr><td class="code"><pre class=" language-hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> n 创建链表节点数</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 返回第一个节点。</span><br><span class="hljs-comment">     */</span><br><span class="hljs-function"><span class="hljs-keyword">static</span> BothDirLinkList <span class="hljs-title">createLinkList</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>{<br>    BothDirLinkList head = <span class="hljs-keyword">new</span> BothDirLinkList();<br>    BothDirLinkList pTerm;<br>    head.value = <span class="hljs-string">"我是头"</span>;<br>    BothDirLinkList qTerm = head;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; n; i++) {<br>        pTerm = <span class="hljs-keyword">new</span> BothDirLinkList();<br>        pTerm.value = Integer.toString(i);<br>        pTerm.last = qTerm;<br>        qTerm.next = pTerm;<br>        qTerm = pTerm;<br>    }<br>    qTerm.next = head;<br>    head.last = qTerm;<br>    <span class="hljs-keyword"><code class="language-hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> n 创建链表节点数</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 返回第一个节点。</span><br><span class="hljs-comment">     */</span><br><span class="hljs-function"><span class="hljs-keyword">static</span> BothDirLinkList <span class="hljs-title">createLinkList</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>{<br>    BothDirLinkList head = <span class="hljs-keyword">new</span> BothDirLinkList();<br>    BothDirLinkList pTerm;<br>    head.value = <span class="hljs-string">"我是头"</span>;<br>    BothDirLinkList qTerm = head;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; n; i++) {<br>        pTerm = <span class="hljs-keyword">new</span> BothDirLinkList();<br>        pTerm.value = Integer.toString(i);<br>        pTerm.last = qTerm;<br>        qTerm.next = pTerm;<br>        qTerm = pTerm;<br>    }<br>    qTerm.next = head;<br>    head.last = qTerm;<br>    <span class="hljs-keyword">return</span> head;<br>}<br></code></pre></td></tr></tbody></table></figure>

<h4 id="3-3-main"><a href="#3-3-main" class="headerlink" title="3.3 main"></a>3.3 main</h4><figure class="highlight java"><table><tbody><tr><td class="code"><pre class=" language-hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{<br>    Scanner sc=<span class="hljs-keyword">new</span> Scanner(System.in);<br><br>    printString(<span class="hljs-string">"创建链表:\n请输入链表节点数："</span>);<br>    BothDirLinkList head = createLinkList(sc.nextInt());<br>    printLinkList(head, <span class="hljs-number">1</span>, <span class="hljs-keyword">true</span>);<br>    <span class="hljs-comment">//合并</span><br>    BothDirLinkList second=createLinkList(<span class="hljs-number">5</span>);<br>    combineLinkList(head,second);<br>    <span class="hljs-comment">//插入</span><br>    printString(<span class="hljs-string">"插入一个值"</span>);<br>    <span class="hljs-comment">//插入的值</span><br>    printString(<span class="hljs-string">"请输入插入的值："</span>);<br>    String value=sc.next();<br>    <span class="hljs-comment">//插入位置</span><br>    printString(<span class="hljs-string">"请输入插入的位置:"</span>);<br>    <span class="hljs-keyword">int</span> pos=sc.nextInt();<br>    <span class="hljs-comment">//插入</span><br>    insertLinkList(head, value, pos);<br>    printLinkList(head, <span class="hljs-number">1</span>, <span class="hljs-keyword">false</span>);<br><br>    <span class="hljs-comment">//删除 BY value</span><br>    printString(<span class="hljs-string">"请输入删除节点的值(不存在时会忽略):\n"</span>);<br>    <span class="hljs-keyword">if</span>(deleteLinkList(head,sc.next())){<br>        printString(<span class="hljs-string">"删除成功!"</span>);<br>        printLinkList(head,<span class="hljs-number">1</span>,<span class="hljs-keyword">false</span>);<br>    }<span class="hljs-keyword">else</span>{<br>        printString(<span class="hljs-string">"删除失败!"</span>);<br>    }<br><br>    <span class="hljs-comment">//删除 BY index</span><br>    printString(<span class="hljs-string">"请输入删除节点的序列:"</span>);<br>    <span class="hljs-keyword">if</span>(deleteLinkList(head,sc.nextInt())){<br>        printString(<span class="hljs-string">"删除成功！"</span>);<br>        printLinkList(head,<span class="hljs-number">1</span>,<span class="hljs-keyword">false</span>);<br>    }<span class="hljs-keyword">else</span>{<br>        printString(<span class="hljs-string"><code class="language-hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{<br>    Scanner sc=<span class="hljs-keyword">new</span> Scanner(System.in);<br><br>    printString(<span class="hljs-string">"创建链表:\n请输入链表节点数："</span>);<br>    BothDirLinkList head = createLinkList(sc.nextInt());<br>    printLinkList(head, <span class="hljs-number">1</span>, <span class="hljs-keyword">true</span>);<br>    <span class="hljs-comment">//合并</span><br>    BothDirLinkList second=createLinkList(<span class="hljs-number">5</span>);<br>    combineLinkList(head,second);<br>    <span class="hljs-comment">//插入</span><br>    printString(<span class="hljs-string">"插入一个值"</span>);<br>    <span class="hljs-comment">//插入的值</span><br>    printString(<span class="hljs-string">"请输入插入的值："</span>);<br>    String value=sc.next();<br>    <span class="hljs-comment">//插入位置</span><br>    printString(<span class="hljs-string">"请输入插入的位置:"</span>);<br>    <span class="hljs-keyword">int</span> pos=sc.nextInt();<br>    <span class="hljs-comment">//插入</span><br>    insertLinkList(head, value, pos);<br>    printLinkList(head, <span class="hljs-number">1</span>, <span class="hljs-keyword">false</span>);<br><br>    <span class="hljs-comment">//删除 BY value</span><br>    printString(<span class="hljs-string">"请输入删除节点的值(不存在时会忽略):\n"</span>);<br>    <span class="hljs-keyword">if</span>(deleteLinkList(head,sc.next())){<br>        printString(<span class="hljs-string">"删除成功!"</span>);<br>        printLinkList(head,<span class="hljs-number">1</span>,<span class="hljs-keyword">false</span>);<br>    }<span class="hljs-keyword">else</span>{<br>        printString(<span class="hljs-string">"删除失败!"</span>);<br>    }<br><br>    <span class="hljs-comment">//删除 BY index</span><br>    printString(<span class="hljs-string">"请输入删除节点的序列:"</span>);<br>    <span class="hljs-keyword">if</span>(deleteLinkList(head,sc.nextInt())){<br>        printString(<span class="hljs-string">"删除成功！"</span>);<br>        printLinkList(head,<span class="hljs-number">1</span>,<span class="hljs-keyword">false</span>);<br>    }<span class="hljs-keyword">else</span>{<br>        printString(<span class="hljs-string">"删除失败!"</span>);<br>    }<br>}<br></code></pre></td></tr></tbody></table></figure>



<h4 id="3-4-插入"><a href="#3-4-插入" class="headerlink" title="3.4 插入"></a>3.4 插入</h4><figure class="highlight java"><table><tbody><tr><td class="code"><pre class=" language-hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> lis 插入数据的链表</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> str 插入的值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> pos 插入位置 可为负，从 head 开始寻找，负数为逆向寻找。</span><br><span class="hljs-comment">     */</span><br><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">insertLinkList</span><span class="hljs-params">(BothDirLinkList lis, String str, <span class="hljs-keyword">int</span> pos)</span> </span>{<br>    BothDirLinkList term = lis;<br>    <span class="hljs-keyword">if</span> (pos == <span class="hljs-number">0</span>) {<br>        printString(<span class="hljs-string">"序列有问题！"</span>);<br>        <span class="hljs-comment">//lis.next.last = new BothDirLinkList(lis, lis.next, str);</span><br>        <span class="hljs-comment">//lis.next = lis.next.last;</span><br>    } <span class="hljs-keyword">else</span> {<br>        <span class="hljs-keyword">int</span> v = pos / Math.abs(pos);<br>        <span class="hljs-comment">// 获取定位节点的移动方向。</span><br>        <span class="hljs-keyword">do</span> {<br>            <span class="hljs-comment">//头指针为 0 序列。所以从lis.next 开始计数。</span><br>            term = (pos &gt; <span class="hljs-number">0</span>) ? term.next : term.last;<br>            pos -= v;<br>        } <span class="hljs-keyword">while</span> (pos != <span class="hljs-number">0</span>);<br>        BothDirLinkList tNode = (v &gt; <span class="hljs-number">0</span>) ? <span class="hljs-keyword">new</span> BothDirLinkList(term.last, term, str) : <span class="hljs-keyword">new</span> BothDirLinkList(term, term.next, str);<br>        <span class="hljs-comment">/* 大于零：插入节点在定位节点之后。</span><br><span class="hljs-comment">             * 小于零：插入节点在定位节点之前。</span><br><span class="hljs-comment">             * */</span><br>        <span class="hljs-keyword">if</span> (v &gt; <span class="hljs-number">0</span>) {<br>            <span class="hljs-comment">/*正向插入</span><br><span class="hljs-comment">                 *</span><br><span class="hljs-comment">                 * 定位节点 后节点 的前驱 为 插入节点</span><br><span class="hljs-comment">                 * 定位节点 的 后驱      为 插入节点</span><br><span class="hljs-comment">                 *   */</span><br>            term.last.next = tNode;<br>            term.last = tNode;<br>        } <span class="hljs-keyword">else</span> {<br>            <span class="hljs-comment">/* 反向插入</span><br><span class="hljs-comment">                 * 原理同上</span><br><span class="hljs-comment"><code class="language-hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> lis 插入数据的链表</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> str 插入的值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> pos 插入位置 可为负，从 head 开始寻找，负数为逆向寻找。</span><br><span class="hljs-comment">     */</span><br><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">insertLinkList</span><span class="hljs-params">(BothDirLinkList lis, String str, <span class="hljs-keyword">int</span> pos)</span> </span>{<br>    BothDirLinkList term = lis;<br>    <span class="hljs-keyword">if</span> (pos == <span class="hljs-number">0</span>) {<br>        printString(<span class="hljs-string">"序列有问题！"</span>);<br>        <span class="hljs-comment">//lis.next.last = new BothDirLinkList(lis, lis.next, str);</span><br>        <span class="hljs-comment">//lis.next = lis.next.last;</span><br>    } <span class="hljs-keyword">else</span> {<br>        <span class="hljs-keyword">int</span> v = pos / Math.abs(pos);<br>        <span class="hljs-comment">// 获取定位节点的移动方向。</span><br>        <span class="hljs-keyword">do</span> {<br>            <span class="hljs-comment">//头指针为 0 序列。所以从lis.next 开始计数。</span><br>            term = (pos &gt; <span class="hljs-number">0</span>) ? term.next : term.last;<br>            pos -= v;<br>        } <span class="hljs-keyword">while</span> (pos != <span class="hljs-number">0</span>);<br>        BothDirLinkList tNode = (v &gt; <span class="hljs-number">0</span>) ? <span class="hljs-keyword">new</span> BothDirLinkList(term.last, term, str) : <span class="hljs-keyword">new</span> BothDirLinkList(term, term.next, str);<br>        <span class="hljs-comment">/* 大于零：插入节点在定位节点之后。</span><br><span class="hljs-comment">             * 小于零：插入节点在定位节点之前。</span><br><span class="hljs-comment">             * */</span><br>        <span class="hljs-keyword">if</span> (v &gt; <span class="hljs-number">0</span>) {<br>            <span class="hljs-comment">/*正向插入</span><br><span class="hljs-comment">                 *</span><br><span class="hljs-comment">                 * 定位节点 后节点 的前驱 为 插入节点</span><br><span class="hljs-comment">                 * 定位节点 的 后驱      为 插入节点</span><br><span class="hljs-comment">                 *   */</span><br>            term.last.next = tNode;<br>            term.last = tNode;<br>        } <span class="hljs-keyword">else</span> {<br>            <span class="hljs-comment">/* 反向插入</span><br><span class="hljs-comment">                 * 原理同上</span><br><span class="hljs-comment">                 * */</span><br>            term.next.last = tNode;<br>            term.next = tNode;<br>        }<br>    }<br>}<br></code></pre></td></tr></tbody></table></figure>



<h4 id="3-5-删除"><a href="#3-5-删除" class="headerlink" title="3.5 删除"></a>3.5 删除</h4><figure class="highlight java"><table><tbody><tr><td class="code"><pre class=" language-hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> lis   操作数组</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> value 删除节点的值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 是否删除成功</span><br><span class="hljs-comment">     */</span><br><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">deleteLinkList</span><span class="hljs-params">(BothDirLinkList lis, String value)</span> </span>{<br>    BothDirLinkList term = lis;<br>    <span class="hljs-keyword">do</span> {<br>        <span class="hljs-keyword">if</span> (term.value.equals(value)) {<br>            <span class="hljs-comment">//中间截断，返回。</span><br>            term.next.last = term.last;<br>            term.last.next = term.next;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>        } <span class="hljs-keyword">else</span> {<br>            term = term.next;<br>        }<br>    } <span class="hljs-keyword">while</span> (term != lis);<br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>}<br><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> lis   操作数组</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> index 删除节点序列 非零整数</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 是否删除成功</span><br><span class="hljs-comment">     */</span><br><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">deleteLinkList</span><span class="hljs-params">(BothDirLinkList lis, <span class="hljs-keyword">int</span> index)</span> </span>{<br>    BothDirLinkList term = lis;<br>    <span class="hljs-keyword">if</span> (index == <span class="hljs-number">0</span>) {<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>    } <span class="hljs-keyword">else</span> {<br>        <span class="hljs-keyword">int</span> v = index / Math.abs(index);<br>        <span class="hljs-comment">//获取定位节点的移动方向。</span><br>        <span class="hljs-keyword">do</span> {<br>            term = (index &gt; <span class="hljs-number">0</span>) ? term.next : term.last;<br>            index -= v;<br>        } <span class="hljs-keyword">while</span> (index != <span class="hljs-number">0</span>);<br><br>        term.next.last = term.last;<br>        term.last.next = term.next;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword"><code class="language-hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> lis   操作数组</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> value 删除节点的值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 是否删除成功</span><br><span class="hljs-comment">     */</span><br><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">deleteLinkList</span><span class="hljs-params">(BothDirLinkList lis, String value)</span> </span>{<br>    BothDirLinkList term = lis;<br>    <span class="hljs-keyword">do</span> {<br>        <span class="hljs-keyword">if</span> (term.value.equals(value)) {<br>            <span class="hljs-comment">//中间截断，返回。</span><br>            term.next.last = term.last;<br>            term.last.next = term.next;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>        } <span class="hljs-keyword">else</span> {<br>            term = term.next;<br>        }<br>    } <span class="hljs-keyword">while</span> (term != lis);<br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>}<br><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> lis   操作数组</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> index 删除节点序列 非零整数</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> 是否删除成功</span><br><span class="hljs-comment">     */</span><br><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">deleteLinkList</span><span class="hljs-params">(BothDirLinkList lis, <span class="hljs-keyword">int</span> index)</span> </span>{<br>    BothDirLinkList term = lis;<br>    <span class="hljs-keyword">if</span> (index == <span class="hljs-number">0</span>) {<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>    } <span class="hljs-keyword">else</span> {<br>        <span class="hljs-keyword">int</span> v = index / Math.abs(index);<br>        <span class="hljs-comment">//获取定位节点的移动方向。</span><br>        <span class="hljs-keyword">do</span> {<br>            term = (index &gt; <span class="hljs-number">0</span>) ? term.next : term.last;<br>            index -= v;<br>        } <span class="hljs-keyword">while</span> (index != <span class="hljs-number">0</span>);<br><br>        term.next.last = term.last;<br>        term.last.next = term.next;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>    }<br>}<br></code></pre></td></tr></tbody></table></figure>



<h4 id="3-6-打印链表"><a href="#3-6-打印链表" class="headerlink" title="3.6 打印链表"></a>3.6 打印链表</h4><figure class="highlight java"><table><tbody><tr><td class="code"><pre class=" language-hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> lis       链表第一个元素</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> isReverse 是否为逆序</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> times     循环打印次数</span><br><span class="hljs-comment">     */</span><br><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printLinkList</span><span class="hljs-params">(BothDirLinkList lis, <span class="hljs-keyword">int</span> times, <span class="hljs-keyword">boolean</span> isReverse)</span> </span>{<br>    BothDirLinkList tr = lis;<br><br>    <span class="hljs-keyword">while</span> (times &gt;= <span class="hljs-number">0</span>) {<br>        <span class="hljs-keyword">if</span> (tr == lis) {<br>            <span class="hljs-keyword">if</span> (times == <span class="hljs-number">0</span>) {<br>                <span class="hljs-keyword">break</span>;<br>            }<br>            times--;<br>        }<br>        System.out.println(<span class="hljs-string">"value = "</span> + tr.value);<br>        tr = isReverse ? tr.last : tr.next;<br>    }<br>    System.out.print(<span class="hljs-string"><code class="language-hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> lis       链表第一个元素</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> isReverse 是否为逆序</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> times     循环打印次数</span><br><span class="hljs-comment">     */</span><br><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printLinkList</span><span class="hljs-params">(BothDirLinkList lis, <span class="hljs-keyword">int</span> times, <span class="hljs-keyword">boolean</span> isReverse)</span> </span>{<br>    BothDirLinkList tr = lis;<br><br>    <span class="hljs-keyword">while</span> (times &gt;= <span class="hljs-number">0</span>) {<br>        <span class="hljs-keyword">if</span> (tr == lis) {<br>            <span class="hljs-keyword">if</span> (times == <span class="hljs-number">0</span>) {<br>                <span class="hljs-keyword">break</span>;<br>            }<br>            times--;<br>        }<br>        System.out.println(<span class="hljs-string">"value = "</span> + tr.value);<br>        tr = isReverse ? tr.last : tr.next;<br>    }<br>    System.out.print(<span class="hljs-string">"\n"</span>);<br>}<br></code></pre></td></tr></tbody></table></figure>



<h4 id="3-7-打印"><a href="#3-7-打印" class="headerlink" title="3.7 打印"></a>3.7 打印</h4><figure class="highlight java"><table><tbody><tr><td class="code"><pre class=" language-hljs java"><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printString</span><span class="hljs-params"><code class="language-hljs java"><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printString</span><span class="hljs-params">(String str)</span></span>{<br>    System.out.println(str);<br>}<br></code></pre></td></tr></tbody></table></figure>


                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">张小白</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://blogofxyz.top/2021/10/29/%E7%BA%BF%E6%80%A7%E8%A1%A8/">https://blogofxyz.top/2021/10/29/%E7%BA%BF%E6%80%A7%E8%A1%A8/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">张小白</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">
                                    <span class="chip bg-color">数据结构</span>
                                </a>
                            
                                <a href="/tags/%E7%BA%BF%E6%80%A7%E8%A1%A8/">
                                    <span class="chip bg-color">线性表</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="qq,qzone,wechat,weibo" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    
    <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid="></script>
    <div class="addthis_inline_share_toolbox"></div>
    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.png" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    
        <style>
    .valine-card {
        margin: 1.5rem auto;
    }

    .valine-card .card-content {
        padding: 20px 20px 5px 20px;
    }

    #vcomments textarea {
        box-sizing: border-box;
        background: url("/medias/comment_bg.JPG") 100% 100% no-repeat;
	    color: white;
    opacity: 0.8;
    font-size: 18px;
    }

    #vcomments p {
        margin: 2px 2px 10px;
        font-size: 1.05rem;
        line-height: 1.78rem;
    }

    #vcomments blockquote p {
        text-indent: 0.2rem;
    }

    #vcomments a {
        padding: 0 2px;
        color: #4cbf30;
        font-weight: 500;
        text-decoration: none;
    }

    #vcomments img {
        max-width: 100%;
        height: auto;
        cursor: pointer;
    }

    #vcomments ol li {
        list-style-type: decimal;
    }

    #vcomments ol,
    ul {
        display: block;
        padding-left: 2em;
        word-spacing: 0.05rem;
    }

    #vcomments ul li,
    ol li {
	text-indent:2rem;
        display: list-item;
        line-height: 2.2rem;
        font-size: 1rem;
    }

    #vcomments ul li {
        list-style-type: disc;
    }

    #vcomments ul ul li {
        list-style-type: circle;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    #vcomments table, th, td {
        border: 0;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments h1 {
        font-size: 1.85rem;
        font-weight: bold;
        line-height: 2.2rem;
    }

    #vcomments h2 {
        font-size: 1.65rem;
        font-weight: bold;
        line-height: 1.9rem;
    }

    #vcomments h3 {
        font-size: 1.45rem;
        font-weight: bold;
        line-height: 1.7rem;
    }

    #vcomments h4 {
        font-size: 1.25rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    #vcomments h5 {
        font-size: 1.1rem;
        font-weight: bold;
        line-height: 1.4rem;
    }

    #vcomments h6 {
        font-size: 1rem;
        line-height: 1.3rem;
    }

    #vcomments p {
        font-size: 1rem;
        line-height: 1.5rem;
    }

    #vcomments hr {
        margin: 12px 0;
        border: 0;
        border-top: 1px solid #ccc;
    }

    #vcomments blockquote {
        margin: 15px 0;
        border-left: 5px solid #42b983;
        padding: 1rem 0.8rem 0.3rem 0.8rem;
        color: #666;
        background-color: rgba(66, 185, 131, .1);
    }

    #vcomments pre {
        font-family: monospace, monospace;
        padding: 1.2em;
        margin: .5em 0;
        background: #272822;
        overflow: auto;
        border-radius: 0.3em;
        tab-size: 4;
    }

    #vcomments code {
        font-family: monospace, monospace;
        padding: 1px 3px;
        font-size: 0.92rem;
        color: #e96900;
        background-color: #f8f8f8;
        border-radius: 2px;
    }

    #vcomments pre code {
        font-family: monospace, monospace;
        padding: 0;
        color: #e8eaf6;
        background-color: #272822;
    }

    #vcomments pre[class*="language-"] {
        padding: 1.2em;
        margin: .5em 0;
    }

    #vcomments code[class*="language-"],
    pre[class*="language-"] {
        color: #e8eaf6;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    #vcomments b,
    strong {
        font-weight: bold;
    }

    #vcomments dfn {
        font-style: italic;
    }

    #vcomments small {
        font-size: 85%;
    }

    #vcomments cite {
        font-style: normal;
    }

    #vcomments mark {
        background-color: #fcf8e3;
        padding: .2em;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }
	.v[data-class="v"] .vwrap .vheader .vinput {
		width: 32%;
		border-bottom: 1px dashed #dedede;
	}
</style>

<div class="card valine-card" data-aos="fade-up">
    <div class="comment_headling" style="font-size: 20px; font-weight: 700; position: relative; padding-left: 20px; top: 15px; padding-bottom: 5px;">
        <i class="fas fa-comments fa-fw" aria-hidden="true"></i>
        <span>评论</span>
    </div>
    <div id="vcomments" class="card-content" style="display: grid">
    </div>
</div>

<script src="/libs/valine/av-min.js"></script>
<script src="/libs/valine/Valine.min.js"></script>
<script>
	let metaPlaceholder = {"nick":"昵称/QQ号(必填)","mail":"邮箱(必填)","link":"网址(https://)"} ;
	
	new Valine({
			el: '#vcomments',
			appId: 'ghConjcbp7CpCRHJFPIRDdd2-gzGzoHsz',
			appKey: 'XnhsRQe0DKrPj7X8NnqYg6BX',
			notify: 'true' === 'true',
			verify: 'true' === 'true',
			visitor: 'true' === 'true',
			avatar: 'identicon',
			pageSize: '10',
			lang: 'zh-CN',
			placeholder: '有事不怕打扰，如果较急，可使用右侧反馈快速接受。',
			meta: ["y-nick", "e-mail", "your-link"],
			recordIP: 'true' === 'true',
			enableQQ: 'identicon',
			requiredFields: ["721850218d5dfa3e2ad5b3ac5a69dad8"],
			master: ["721850218d5dfa3e2ad5b3ac5a69dad8"],
			friends: ["4802978b7156ffcf24b571b35c594ad7"],
			tagMeta: ["发文地", "认识地", "过路地"],
			metaPlaceholder: metaPlaceholder,
		});
</script>

<!--酷Q推送-->


    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2021/10/29/%E9%80%92%E5%BD%92%E7%AE%97%E6%B3%95/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/16.jpg" class="responsive-img" alt="递归算法">
                        
                        <span class="card-title">递归算法</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            B站的一次活动，做的算法题，突然就想到递归方法，秒解。
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-10-29
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/C%E8%AF%AD%E8%A8%80/" class="post-category">
                                    C语言
                                </a>
                            
                            <a href="/categories/C%E8%AF%AD%E8%A8%80/%E7%AE%97%E6%B3%95/" class="post-category">
                                    算法
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/C%E8%AF%AD%E8%A8%80/">
                        <span class="chip bg-color">C语言</span>
                    </a>
                    
                    <a href="/tags/%E9%80%92%E5%BD%92%E7%AE%97%E6%B3%95/">
                        <span class="chip bg-color">递归算法</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2021/10/26/%EF%BC%88%E8%A1%A5%E5%85%85%E4%B8%AD%EF%BC%89%E7%BD%91%E7%BB%9C&%E8%BD%AF%E4%BB%B6%E7%BC%A9%E5%86%99/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/14.jpg" class="responsive-img" alt="（补充中）名词缩写">
                        
                        <span class="card-title">（补充中）名词缩写</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            对计算机网络、与一些语言中的名词进行的整理。
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-10-26
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/" class="post-category">
                                    计算机网络
                                </a>
                            
                            <a href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/%E8%BD%AF%E4%BB%B6%E5%B7%A5%E7%A8%8B/" class="post-category">
                                    软件工程
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E5%90%8D%E8%AF%8D%E8%8B%B1%E6%96%87%E7%BC%A9%E5%86%99/">
                        <span class="chip bg-color">名词英文缩写</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE' || selection.getRangeAt(0).commonAncestorContainer.nodeName === 'CODE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: X-Y-Z`s Blog<br />'
            + '文章作者: Zhang-little-white<br />'
            + '文章链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('2'),
            headingSelector: 'h1,h2, h3, h4, h5'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1,h2, h3, h4, h5').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2022</span>
            
            <span id="year">2021</span>
            <a href="/about" target="_blank">Zhang-little-white</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">53.2k</span>
            
            
            
            
            
            <br>

            <!-- 运行天数提醒. -->
            
                <span id="sitetime"> Loading ...</span>
                <script>
                    var calcSiteTime = function () {
                        var seconds = 1000;
                        var minutes = seconds * 60;
                        var hours = minutes * 60;
                        var days = hours * 24;
                        var years = days * 365;
                        var today = new Date();
                        var startYear = "2021";
                        var startMonth = "8";
                        var startDate = "12";
                        var startHour = "0";
                        var startMinute = "0";
                        var startSecond = "0";
                        var todayYear = today.getFullYear();
                        var todayMonth = today.getMonth() + 1;
                        var todayDate = today.getDate();
                        var todayHour = today.getHours();
                        var todayMinute = today.getMinutes();
                        var todaySecond = today.getSeconds();
                        var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                        var diff = t2 - t1;
                        var diffYears = Math.floor(diff / years);
                        var diffDays = Math.floor((diff / days) - diffYears * 365);

                        // 区分是否有年份.
                        var language = 'zh-CN';
                        if (startYear === String(todayYear)) {
                            document.getElementById("year").innerHTML = todayYear;
                            var daysTip = 'This site has been running for ' + diffDays + ' days';
                            if (language === 'zh-CN') {
                                daysTip = '本站已运行 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                daysTip = '本站已運行 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = daysTip;
                        } else {
                            document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                            var yearsAndDaysTip = 'This site has been running for ' + diffYears + ' years and '
                                + diffDays + ' days';
                            if (language === 'zh-CN') {
                                yearsAndDaysTip = '本站已运行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                yearsAndDaysTip = '本站已運行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = yearsAndDaysTip;
                        }
                    }
                    calcSiteTime();
                </script>
            
            <br>
            
                <span id="icp">
				<img src="/medias/icp.png"豫ICP备2021025736号
                                    style="vertical-align: text-bottom;"/>
                <a href="https://beian.miit.gov.cn" target="_blank">豫ICP备2021025736号</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link ">
    <a href="https://github.com/xiaohuixian" class="tooltipped" target="_blank" data-tooltip="visit my GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:1970476603@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1970476603" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1970476603" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
				
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>
<div>
<a onclick="switchNightMode()" id="sma" title="模式切换">
    <i class="fa fa-moon-o" id="nightMode" aria-hidden="true"></i>
</a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>
    <script src="/libs/mermaid/mermaid.min.js"></script>

    <!-- Baidu Analytics -->

<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b3593488200a34a873defcaa11fc888a";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    

    

    

    <!--腾讯兔小巢-->
    
        <div style="position:fixed;bottom:125px;right:16px;cursor: pointer;" ID="tuxiaochao">
            <a title="兔小巢" target="_blank" rel="noopener" href="https://support.qq.com/products/348756"><i class="fa fa-comments fa-3x"  aria-hidden="true"></i></a>
        </div>
    
    

    
    
    <script type="text/javascript" size="150" alpha='0.6'
        zIndex="-1" src="/libs/background/ribbon-refresh.min.js" async="async"></script>
    

    
    <script type="text/javascript" src="/libs/background/ribbon-dynamic.js" async="async"></script>
    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    
	<script type="text/javascript">
//	//只在桌面版网页启用特效
	//var windowWidth = $(window).width();
	//if (windowWidth > 768) {
//		document.write('<script type="text/javascript" src="/js/sakura.js"><\/script>');
//	}
	</script>
	
	<script type="text/javascript">
		var OriginTitile=document.title,st;
		document.addEventListener("visibilitychange",function(){ document.hidden?(document.title="ヽ(●-`Д´-)ノ110，有人失踪了",clearTimeout(st)):(document.title="(Ő∀Ő3)ノ失踪人口回归！",st=setTimeout(function(){document.title=OriginTitile},3e3))
		})
	</script>


</body>

</html>
